﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.SessionState;
using GoDinner.Models;
using System.Web.Security;
using GoDinner.Filters;
using System.Globalization;
using GoDinner.Models;

namespace GoDinner.Controllers
{
    [InitializeSimpleMembership]
    public class ReservationController : Controller
    {
        Models.GoDinnerDbContext _db = new Models.GoDinnerDbContext();

        //
        // GET: /Reservation/
        [Authorize]
        public ActionResult Index()
        {
            return View();
        }

        [Authorize]
        public ActionResult GetCartView()
        {
            Cart c = GetCart();
            return PartialView("_Cart", c);
        }

        [Authorize]
        public ActionResult Order()
        {
            Cart c = GetCart();
            Gerecht gerecht = _db.Gerechten.FirstOrDefault(g => g.Id == 1);
            var model = _db.GerechtTypes.ToList();
            return View(model);
        }

        [Authorize]
        public ActionResult RemoveGerecht(int gerechtId)
        {
            Gerecht gerecht = _db.Gerechten.FirstOrDefault(x => x.Id == gerechtId);
            Cart c = GetCart();
            c.DecreaseQuantity(gerecht);
            return Content(gerecht.Id + " removed 1");
        }

        [Authorize]
        private void CreateOrder(DateTime datetime)
        {
            Cart c = GetCart();

            List<ReserveringDetails> details = new List<ReserveringDetails>();
            foreach (CartLine line in c.GetCartLines())
            {
                details.Add(new ReserveringDetails
                {
                    GerechtId = line.Gerecht.Id,
                    Hoeveelheid = line.Quantity,
                    Prijs = line.Gerecht.Prijs,
                    Totaal = line.Gerecht.Prijs * line.Quantity
                });
            }
            var user = User.Identity.Name;
            _db.Reserveringen.Add(new Reservering
            {
                IsGoedgekeurd = false,
                ReserveringDetails = details,
                UserId = (int)Membership.GetUser().ProviderUserKey,
                TotaalBedrag = c.GetTotal(),
                datum = datetime

            });

            _db.SaveChanges();
        }


        [HttpGet]
        [Authorize]
        public ActionResult CheckOut()
        {
            Cart c = GetCart();
            if (c.GetTotal() == 0.0)
            {
                return Redirect("/Reservation/Order");
            }
            return View();
        }

        [HttpPost]
        [Authorize]
        public ActionResult CheckOut(string reservationDate, string reservationHour, string reservationMinute)
        {
            
            Cart c = GetCart();
            if (c.GetTotal() == 0.0)
            {
                return Redirect("/Reservation/Order");
            }
            string dt = reservationDate + " " + reservationHour + ":" + reservationMinute;
            string format = "dd/MM/yyyy HH:mm";
            DateTime date;

            try
            {
                date = DateTime.ParseExact(dt, format, CultureInfo.InvariantCulture);
            }
            catch (FormatException e)
            {
                ViewBag.error = e.Message;
                return View();
            }

            if (date < DateTime.Today)
            {
                ViewBag.error = "Datum kan niet in het verleden liggen";
                return View();
            }

            CreateOrder(date);
            c.Clear();
            return Redirect("/Reservation/Success");
        }
            
        public ActionResult Success()
        {
            return View();
        }

        [Authorize]
        public ActionResult AddGerecht(int gerechtId)
        {
            Gerecht gerecht = _db.Gerechten.FirstOrDefault(x => x.Id == gerechtId);

            Cart c = GetCart();
            c.AddGerecht(gerecht, 1);

            return Content(gerecht.Id + " added 1");
        }

        [Authorize]
        private Cart GetCart()
        {

            Cart cart = (Cart)Session["Cart"];
            if (cart == null)
            {
                cart = new Cart();
                Session["Cart"] = cart;
            }
            return cart;
        }

        [Authorize(Roles = "Admin")]
        public ActionResult OverzichtOrders()
        {
            var model = _db.Reserveringen.ToList();
            return View(model);
        }


        [HttpGet]
        [Authorize(Roles = "Admin")]
        public ActionResult OrderDetail(int id)
        {
            var model = _db.Reserveringen.Find(id);
            return View(model);
        }

        [HttpGet]
        [Authorize(Roles = "Admin")]
        public ActionResult Approve(int id)
        {
            var model = _db.Reserveringen.Find(id);
            model.IsGoedgekeurd = true;
            _db.SaveChanges();

            /**
             * Hier zou mail verzonden moeten worden naar klant
             **/

            return Redirect("/Reservation/OverzichtOrders");
        }



    }
}
